package org.dtrd.modules.api.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.patient.entity.bean.DtrdEntRdFlupRecordDTO;
import org.dtrd.modules.patient.entity.po.DtrdEntRdFlupRecord;
import org.dtrd.modules.patient.service.IDtrdEntRdFlupRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 复诊随访接口
 * @author zcli
 * @date 2021-09-25
 */
@Api(tags = "复诊随访记录")
@Slf4j
@RestController
@RequestMapping("/104")
public class FlupAPIController extends BaseController {

    @Autowired
    private IDtrdEntRdFlupRecordService flupRecordService;

    /**
     * 01添加/修改随访记录
     * 添加/修改一条随访复诊记录
     * @param
     */
    @ApiOperation("添加/修改随访记录")
    @PostMapping("/01/1040101")
    public Result<?> doFlupRecord(@RequestBody DtrdEntRdFlupRecordDTO flupRecordDTO){
        try {
            if (null != flupRecordDTO) {
                Integer dataId = flupRecordDTO.getDataId();
                Integer patientId = flupRecordDTO.getPatientId();
                if (patientId == null) {
                    return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
                }
                if (dataId == null) { // 没有传入 dataId, 添加情况
                    DtrdEntRdFlupRecord flupRecord = new DtrdEntRdFlupRecord();
                    flupRecord.setCreateTime(new Date());
                    BeanCopier beanCopier = BeanCopier.create(DtrdEntRdFlupRecordDTO.class, DtrdEntRdFlupRecord.class, false);
                    beanCopier.copy(flupRecordDTO, flupRecord, null);
                    flupRecordService.save(flupRecord);
                }
                if (dataId != null && dataId >= 1){
                    DtrdEntRdFlupRecord flupRecord = flupRecordService.getOne(Wrappers.lambdaQuery(DtrdEntRdFlupRecord.class)
                            .eq(DtrdEntRdFlupRecord::getDataId, dataId).eq(DtrdEntRdFlupRecord::getPatientId, patientId)
                            .eq(DtrdEntRdFlupRecord::getIsDel, 0));
                    if (flupRecord == null) { // 正常情况下 dataId 都是由前端查询传入, 不回没有数据
                        return error(SystemState.ResponseState.STATE_NO_RECORD);
                    }else {
                        BeanCopier beanCopier = BeanCopier.create(DtrdEntRdFlupRecordDTO.class, DtrdEntRdFlupRecord.class, false);
                        beanCopier.copy(flupRecordDTO, flupRecord, null);
                        flupRecordService.updateById(flupRecord);
                    }
                }
                return Result.OK();
            } else {
                return Result.error(SystemState.ResponseState.STATE_PARAMS_ERROR.getCode(), SystemState.ResponseState.STATE_PARAMS_ERROR.getMsg());
            }
        } catch (Exception e) {
            log.error("编辑患者健康档案-添加/修改随访记录异常", e);
            return Result.error(SystemState.ResponseState.STATE_OPERATION_FAILURE.getCode(), SystemState.ResponseState.STATE_OPERATION_FAILURE.getMsg());
        }
    }

    /**
     * 02查询随访记录
     * 最近一次的随访记录对象及以前所有记录的随访记录Id集合
     * @param patientId
     * @return
     */
    @ApiOperation("查询随访记录")
    @GetMapping("/01/1040102")
    public Result<?> getFlupRecord(@RequestParam Integer patientId){
        try {
            List<DtrdEntRdFlupRecord> flupRecordList = flupRecordService.list(Wrappers.lambdaQuery(DtrdEntRdFlupRecord.class)
                    .eq(DtrdEntRdFlupRecord::getPatientId, patientId).orderByDesc(DtrdEntRdFlupRecord::getCreateTime));
            if (null != flupRecordList && !flupRecordList.isEmpty()) {
                DtrdEntRdFlupRecord flupRecord = flupRecordList.get(0);
                DtrdEntRdFlupRecordDTO flupRecordDTO = new DtrdEntRdFlupRecordDTO();
                BeanCopier beanCopier = BeanCopier.create(DtrdEntRdFlupRecord.class, DtrdEntRdFlupRecordDTO.class, false);
                beanCopier.copy(flupRecord, flupRecordDTO, null);
                // 结果映射
                Map<String, Object> resultMap = new HashMap<>(2);
                resultMap.put("flupRecord", flupRecordDTO);
                List<Integer> ids = flupRecordList.stream()
                        .map(DtrdEntRdFlupRecord::getDataId)
                        .filter(dataId -> !Objects.equals(dataId, flupRecord.getDataId()))
                        .collect(Collectors.toList());
                resultMap.put("ids", ids);
                return Result.ok(resultMap);
            } else {
                return Result.error(SystemState.ResponseState.STATE_NO_RECORD.getCode(), SystemState.ResponseState.STATE_NO_RECORD.getMsg());
            }
        } catch (Exception e) {
            log.error("获取患者健康档案-查询随访记录异常", e);
            return Result.error(SystemState.ResponseState.STATE_OPERATION_FAILURE.getCode(), SystemState.ResponseState.STATE_OPERATION_FAILURE.getMsg());
        }
    }

}
